﻿/*
 *Copyright(C) 2020 by  GYYX All rights reserved.
 *Unity版本：2018.4.23f1 
 *作者:程一峰  
 *创建日期: 2021-04-08 
 *模块说明：
 *版本: 1.2
*/

using UnityEditor;
using UnityEngine;
using Warfare.FSM;

namespace WarfareEditor.FSM
{
    /// <summary>
    /// 配置窗口的鼠标操作处理
    /// </summary>
    public class FSMZoom_DesignMouseHandler : FSMZoom
    {

        protected internal override void Init(FSMEditorWindow aWindow)
        {
            base.Init(aWindow);
            m_NodeContext = FSM_AddNodeInfo.Create();
            FSM_AddNodeInfo.Handler = this;
        }

        private FSM_AddNodeInfo m_NodeContext;

        protected internal override void OnDestroy() { }

        private Vector2 mousePosition;
        internal Vector2 MousePosition { get { return mousePosition; } }
        private FSMNode selectNode;

        /// <summary>
        /// 鼠标左键按下时选中的节点；
        /// </summary>
        private FSMNode downSelectNode
        {
            get { return window.CurrentNode; }
            set { window.CurrentNode = value; }
        }

        private Vector2 mouseDownPostionOffset;

        protected internal override void OnGUI()
        {
            if (window.CurrentAsset == null)
                return;

            Event mEvent = Event.current;
            mousePosition = mEvent.mousePosition;
            selectNode = FSMEditorTool.FindMouseNode(window, mousePosition);

            switch (mEvent.type)
            {
                case EventType.MouseDown:
                    if (mEvent.button == 0)
                    {
                        downSelectNode = selectNode;
                        if (downSelectNode != null)
                            mouseDownPostionOffset = mousePosition - downSelectNode.NodeRect.position;
                        else
                            mouseDownPostionOffset = Vector2.zero;
                        OnConnectEnd();
                    }
                    else
                        downSelectNode = null;

                    break;
                case EventType.MouseDrag:
                    if (mEvent.button == 0)
                        DragNode();
                    break;
                case EventType.MouseUp:
                    if (mEvent.button == 1)
                        ShowMenu();
                    break;
                default:
                    DrawConnectLine();
                    break;
            }
        }

        #region 鼠标左键

        /// <summary>
        /// 拖动节点；
        /// </summary>
        private void DragNode()
        {
            if (downSelectNode == null)
                return;

            downSelectNode.SetNodePosition(mousePosition - mouseDownPostionOffset);
        }

        #endregion

        #region 鼠标右键；

        private GUIContent contentConnectSucess = new GUIContent("成功后节点");
        private GUIContent contentConnectFailed = new GUIContent("失败后节点");
        private GUIContent contentEditNode = new GUIContent("编辑节点");
        private GUIContent contentRemoveNode = new GUIContent("删除节点");

        private FSMNode rightClickNode;

        private void ShowMenu()
        {
            GenericMenu menu = new GenericMenu();
            rightClickNode = selectNode;
            if (selectNode == null)
            {
                //右击空地弹出的菜单
                menu.AddItem(m_NodeContext.Content, false, m_NodeContext.AddNode);
            }
            else
            {
                //右击到某个具体节点的弹出菜单；
                menu.AddItem(contentConnectSucess, selectNode.IsHasSucessNode(), ConnectSucess);
                menu.AddItem(contentConnectFailed, selectNode.IsHasFailedNode(), ConnectFailed);
                if (!(rightClickNode is FSMRootNode))
                {
                    menu.AddItem(contentEditNode, false, EditNode);
                    menu.AddItem(contentRemoveNode, false, RemoveNode);
                }
            }

            menu.ShowAsContext();
            Event.current.Use();
        }

        internal void AddNode(FSMNode newNode) { window.CurrentAsset.Do_AddNode(newNode); }

        private void RemoveNode()
        {
            Debug.Log($"删除节点：{selectNode.NodeName}");
            window.CurrentAsset.Do_RemoveNode(selectNode);
            rightClickNode = null;
        }

        /// <summary>
        /// 编辑节点；
        /// </summary>
        private void EditNode()
        {
            FSMNodeEditorWindow.ShowWindow(rightClickNode);
            rightClickNode = null;
        }

        private bool ConnectingSucess;
        /// <summary>
        /// 成功后连接
        /// </summary>
        private void ConnectSucess()
        {
            ConnectingSucess = true;
            ConnectingFailed = false;

            if (rightClickNode.IsHasSucessNode())
            {
                rightClickNode.SetSucessNode(null);
                ConnectingSucess = false;
            }
        }

        private bool ConnectingFailed;
        /// <summary>
        /// 失败后连接；
        /// </summary>
        private void ConnectFailed()
        {
            ConnectingFailed = true;
            ConnectingSucess = false;

            if (rightClickNode.IsHasFailedNode())
            {
                rightClickNode.SetFailedNode(null);
                ConnectingSucess = false;
            }
        }

        private void DrawConnectLine()
        {
            if (rightClickNode == null)
                return;

            //开始划线。。。
            Vector2 startPos;
            Color col;
            if (ConnectingSucess)
            {
                startPos = rightClickNode.SucessPoint();
                col = new Color(0.5f, 1, 0.5f, 1);
            }
            else if (ConnectingFailed)
            {
                startPos = rightClickNode.FailedPoint();
                col = new Color(1, 0.5f, 0.5f, 1);
            }
            else
                return;

            FSMEditorTool.DrawLine(startPos, mousePosition, col);
        }

        private void OnConnectEnd()
        {
            if (downSelectNode != null && rightClickNode != null)
            {
                if (ConnectingSucess)
                    rightClickNode.SetSucessNode(downSelectNode);
                if (ConnectingFailed)
                    rightClickNode.SetFailedNode(downSelectNode);
            }

            ConnectingSucess = false;
            ConnectingFailed = false;
        }

        #endregion
    }
}